Ext.define('js.inner.PeopleManagementShowPage', {
    extend: 'Ext.grid.Panel',
    id:'PeopleManagementShowPage',
    requires: ['js.Util', 'js.ctrl.Common',],
    controller: 'Common',
    autoScroll : true,
    margin:30,
    height:608,
    bodyStyle: {
        backgroundColor: '#eee',
    },
    userGroupForm: function () {
        return Ext.create('js.user.DepartmentForm');
    },
    initComponent: function () {
        this.store = Ext.create('js.store.inner.PeopleManagementListStore');
        this.store.getProxy().setExtraParam('pageNum', 1);
        this.store.load({
            callback:function () {
                let me = Ext.getCmp('PeopleManagementShowPage');
                if(me){
                    if(me.store.data.items.length>0){
                        new Page({
                            id: 'PeopleManagementSupplier',
                            pageTotal: me.store.data.items[0].data.pageTotal, //必填,总页数
                            pageAmount: 10,  //每页多少条
                            dataTotal: me.store.data.items[0].data.dataTotal, //总共多少条数据
                            curPage:parseInt(me.store.data.items[0].data.pageNum), //初始页码,不填默认为1
                            pageSize: 5, //分页个数,不填默认为5
                            showPageTotalFlag:true, //是否显示数据统计,不填默认不显示
                            showSkipInputFlag:true, //是否支持跳转,不填默认不显示
                            getPage: function (page) {
                                //获取当前页数
                                me.store.getProxy().setExtraParam('pageNum', page);
                                me.store.reload();
                            }
                        })
                    }else{
                        new Page({
                            id: 'PeopleManagementSupplier',
                            pageTotal: 1, //必填,总页数
                            pageAmount: 10,  //每页多少条
                            dataTotal: 0, //总共多少条数据
                            pageSize: 5, //分页个数,不填默认为5
                            showPageTotalFlag:true, //是否显示数据统计,不填默认不显示
                            showSkipInputFlag:true, //是否支持跳转,不填默认不显示
                            getPage: function (page) {
                                //获取当前页数
                                me.store.getProxy().setExtraParam('pageNum', page);
                                me.store.reload();
                            }
                        })
                    }
                }
            }
        });
        this.columns = [
            {text: '人员名称', dataIndex: 'user_name', align: 'center', flex: 1},
            {text: '所属部门', dataIndex: 'department_name', align: 'center', flex: 1},
            {text: '手机（系统登陆名）', dataIndex: 'mobile', align: 'center', flex: 1,},
            {text: '角色', dataIndex: 'group_name', align: 'center', flex: 1,},
            {text: '是否启用系统账号', dataIndex: 'enable_char', align: 'center', flex: 1,},
            {
                text: "操作", dataIndex: 'user_id',
                flex:2,
                align: 'center',
                renderer: function (value, cellmeta, record, rowIndex, columnIndex, store) {//自定义列值组合

                    return '<span style="margin-right:20px;color:#2672d9;font-size: 17px;letter-spacing: 1px;cursor:pointer;"><a href="#"  style="text-decoration:none;" onclick="PeopleUpd(' + JSON.stringify(record.data).replace(/\"/g, "'") + ')">编辑</a></span> <span style="margin-right:20px;color:#ff8041;font-size: 17px;letter-spacing: 1px;cursor:pointer;"> <a href="#" style="color:#ff8041;text-decoration:none;" onclick="initPassword(' + JSON.stringify(record.data).replace(/\"/g, "'") + ')">密码重置</a></span>';
                },height:44
            }
        ];
        this.dockedItems = [
            {
                dock: 'top',
                xtype: 'toolbar',
                border:'none',
                margin:'0 0 20 0',
                items: [
                    {
                        xtype : "button",
                        text:'添加',
                        cls:'button-Intelligent-sensor',
                        style:"cursor:pointer",
                        margin:'0 5% 0 0%',
                        width: '9.5%',
                        scope: this,
                        handler: this.peopleMergerAdd,
                        height: 32,
                        action_type: 'personAdd',
                    },{
                        xtype : "button",
                        text:'删除',
                        cls:'button-Intelligent-sensorA',
                        style:"cursor:pointer",
                        margin:'0 5% 0 29%',
                        width: '9.5%',
                        handler: this.projectMergerDel,
                        height: 32,
                    },
                    {
                        xtype : "button",
                        text:'刷新',
                        cls:'button-Intelligent-sensor',
                        style:"cursor:pointer",
                        margin:'0 5% 0 29%',
                        handler: this.refresh,
                        scope: this,
                        width: '9.5%',
                        height: 32,
                    }
                ]

            },{
                xtype: 'panel',
                html:'<div id="PeopleManagementSupplier" class="pagination"></div>',
                dock: 'bottom',
                height:50,
                margin:'-14 0 0 0',
                displayInfo: true,
            }]
        this.selModel = Ext.create('Ext.selection.CheckboxModel', {mode: 'MULTI', text: '全选'});
        this.callParent(arguments);
    },
    peopleMergerAdd: function (btn) {
        let params = {
            action_type: btn.action_type
        };
        let btnCancel = Ext.create('Ext.button.Button', {
                    cls: 'btnCancel',
                    text: `取消`,
                    scale: 'medium',
                    width:132,
                    height:42,
                    margin:'0 0 20 200',
                    handler: function (btn) {
                        btn.up('window').close();
            },
            scope: this

        });
        let btnSubmit = Ext.create('Ext.button.Button', {
            cls:'btnSubmit',
            border:false,
            text: `保存`,
            margin: '0 0 20 30',
            width:132,
            height:42,
            scale: 'medium',
            handler: function (btn) {
                    let user_name=Ext.getCmp('user_name').getValue();
                    let mobile=Ext.getCmp('mobile').getValue();
                    let group_id=Ext.getCmp('group_id').getValue();
                    let enable_char=Ext.getCmp('enable_char').getValue()
                if(user_name===''||mobile===''){
                    Ext.toast('请将信息填写完整');
                }else if(/^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/.test(mobile)==false){
                    Ext.toast('手机号码不正确');
                }else{
                    util.post('/publicUserManagementRoutes', {
                        action_type: params.action_type,
                        user_name: user_name,
                        mobile: mobile,
                        group_id: group_id,
                        enable_char: enable_char
                    }).then(function (response) {
                        let obj = Ext.decode(response.responseText);
                        if (obj.success) {
                            Ext.toast('保存成功');
                            let me = Ext.getCmp('PeopleManagementShowPage');
                            me.store.reload();
                            btn.up('window').close();
                        } else {
                            Ext.toast(`<div style="font-size:28px;">保存失败,${obj.message}</div>`);
                        }
                    });
                }
            },
            scope: this
        });
        let formPanel = Ext.create('js.inner.PeopleMergerAddPage', {
            hidenField: params,
            id: 'PeopleMergerAddPage'
        });
        let win = util.window(formPanel, {title:'添加人员列表',width:'40%',height:'45%   ', buttons: ['->', btnCancel, btnSubmit, '->']});
        win.show();
    },
    //删除
    deleteManager: function () {
        let resultSelection = this.getSelection();
        let ids = Ext.Array.map(resultSelection, (item) => {
            return item.get('user_id')
        });
        util.get('/publicUserManagementRoutes', {
            action_type: 'personDel',
            user_id: ids.join(',')
        }).then(this.delSuccess, this.delFail);
    },
    projectMergerDel: function (btn) {
        let me = Ext.getCmp('PeopleManagementShowPage');
        let userSelection = me.getSelection();
        if (userSelection.length <= 0) {
            Ext.toast('请选择需要删除的项目');
        } else {
            util.deleteConfirm(function () {
                util.prompt(me.deleteManager, me);
            })
        }
    },
    delSuccess: function (response) {
        let obj = Ext.decode(response.responseText);
        if (obj.success) {
            Ext.toast('删除成功');
            Ext.getCmp('PeopleManagementShowPage').getStore().reload();
        } else {
            Ext.toast(obj.message);
        }
    },
    delFail: function (response) {
        Ext.toast(`<div style="font-size:28px">删除失败：${response.status}</div>`);
    },
    refresh: function (btn) {
        this.getStore().reload();
    }

});